<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简化登录测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        form {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .log-container {
            border: 1px solid #ddd;
            border-radius: 4px;
            max-height: 400px;
            overflow-y: auto;
            background-color: #f9f9f9;
            padding: 10px;
            font-family: monospace;
            font-size: 14px;
            margin-top: 20px;
        }
        .log-entry {
            margin-bottom: 5px;
            padding: 3px;
        }
        .log-info {
            color: #333;
        }
        .log-error {
            color: #e74c3c;
            background-color: #fadbd8;
        }
        .log-success {
            color: #27ae60;
        }
        .log-debug {
            color: #2980b9;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>简化登录测试</h1>
        
        <form id="loginForm">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" value="admin" required>
            
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" value="abc@123456" required>
            
            <button type="submit">测试登录</button>
        </form>
        
        <div class="log-container" id="logContainer">
            <div class="log-entry log-info">请点击"测试登录"按钮开始测试...</div>
        </div>
    </div>

    <script>
        // 日志工具
        const logContainer = document.getElementById('logContainer');
        
        function log(message, type = 'info') {
            const logEntry = document.createElement('div');
            logEntry.className = `log-entry log-${type}`;
            logEntry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
            logContainer.appendChild(logEntry);
            logContainer.scrollTop = logContainer.scrollHeight;
        }
        
        function logObject(obj, prefix = '') {
            try {
                const str = JSON.stringify(obj, null, 2);
                str.split('\n').forEach(line => {
                    log(`${prefix}${line}`, 'debug');
                });
            } catch (e) {
                log(`${prefix}无法序列化对象: ${e.message}`, 'error');
            }
        }
        
        // 简化的登录测试函数
        async function testDirectLogin(username, password) {
            log('===== 开始简化登录测试 =====', 'info');
            
            try {
                // 构建请求参数
                const params = new URLSearchParams();
                params.append('username', username);
                params.append('password', password);
                
                log(`登录参数: ${params.toString()}`, 'info');
                
                // 测试1: 通过Vite代理访问
                log('\n测试1: 通过Vite代理访问 /api/auth/token', 'info');
                try {
                    log('发送请求...', 'debug');
                    const startTime = Date.now();
                    
                    const response1 = await fetch('/api/auth/token', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        },
                        body: params
                    });
                    
                    const endTime = Date.now();
                    log(`收到响应，状态码: ${response1.status}，耗时: ${endTime - startTime}ms`, 'debug');
                    
                    if (!response1.ok) {
                        throw new Error(`HTTP错误! 状态码: ${response1.status}`);
                    }
                    
                    const data1 = await response1.json();
                    log('响应数据解析成功', 'success');
                    log(`响应数据类型: ${typeof data1}`, 'debug');
                    if (typeof data1 === 'object') {
                        log(`响应数据包含的字段: ${Object.keys(data1).join(', ')}`, 'debug');
                        logObject(data1, '  ');
                    }
                    
                    // 检查是否包含必要的字段
                    if (data1 && data1.access_token && data1.user) {
                        log('✓ 测试1成功: 响应包含所有必要的字段', 'success');
                    } else {
                        log('✗ 测试1失败: 响应缺少必要的字段', 'error');
                    }
                } catch (error) {
                    log(`测试1失败: ${error.message}`, 'error');
                }
                
                // 测试2: 直接访问后端API
                log('\n测试2: 直接访问后端API http://localhost:8000/auth/token', 'info');
                try {
                    log('发送请求...', 'debug');
                    const startTime = Date.now();
                    
                    const response2 = await fetch('http://localhost:8000/auth/token', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        },
                        body: params
                    });
                    
                    const endTime = Date.now();
                    log(`收到响应，状态码: ${response2.status}，耗时: ${endTime - startTime}ms`, 'debug');
                    
                    if (!response2.ok) {
                        throw new Error(`HTTP错误! 状态码: ${response2.status}`);
                    }
                    
                    const data2 = await response2.json();
                    log('响应数据解析成功', 'success');
                    log(`响应数据类型: ${typeof data2}`, 'debug');
                    if (typeof data2 === 'object') {
                        log(`响应数据包含的字段: ${Object.keys(data2).join(', ')}`, 'debug');
                        logObject(data2, '  ');
                    }
                    
                    // 检查是否包含必要的字段
                    if (data2 && data2.access_token && data2.user) {
                        log('✓ 测试2成功: 响应包含所有必要的字段', 'success');
                    } else {
                        log('✗ 测试2失败: 响应缺少必要的字段', 'error');
                    }
                } catch (error) {
                    log(`测试2失败: ${error.message}`, 'error');
                }
                
            } catch (error) {
                log(`登录测试过程中发生错误: ${error.message}`, 'error');
            } finally {
                log('===== 简化登录测试结束 =====', 'info');
            }
        }
        
        // 表单提交处理
        document.getElementById('loginForm').addEventListener('submit', function(event) {
            event.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            // 清空日志容器
            logContainer.innerHTML = '';
            
            // 执行登录测试
            testDirectLogin(username, password);
        });
        
        log('简化登录测试页面已加载完成，请点击"测试登录"按钮开始测试。', 'info');
    </script>
</body>
</html>